<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-sys__dept">
      <el-row :gutter="20">
        <!-- 侧边栏 -->
        <el-col :sm="8">
          <div>
            <el-button @click="add" size="mini" icon="el-icon-plus" type="primary" round>添加子部门</el-button>
            <el-button @click="addRoot" size="mini" icon="el-icon-plus" type="primary" round>添加一级部门</el-button>
            <el-button @click="del" size="mini" icon="el-icon-delete" type="danger" round>删除</el-button>
            <el-button @click="getDeptDatas" size="mini" icon="el-icon-refresh" type="success" round>刷新</el-button>
          </div>
          <div class="tree">
            <el-tree :data="depts" :props="defaultProps" :expand-on-click-node="false" :highlight-current="true" default-expand-all @node-click="handleNodeClick" />
          </div>
        </el-col>
        <!-- 侧边栏 -->

        <!-- 中间数据 -->
        <el-col :sm="16">
          <el-form label-width="80px" :rules="rules" :model="form" ref="form">
            <el-form-item label="上级部门" prop="parentName">
              <el-input v-model="form.parentName" placeholder="请输入父级节点"></el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" :min="1" :max="100" label="请输入排序"></el-input-number>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="save">保存</el-button>
              <el-button @click="cancel">重置</el-button>
            </el-form-item>
          </el-form>
        </el-col>
        <!-- 中间数据 -->

      </el-row>

      <el-dialog :title="modalTitle" :visible.sync="modalVisible">
        <el-form ref="formAdd" :model="formAdd" label-width="85px" :rules="rules">
          <el-form-item v-if="formAdd.pid" label="上级部门">
            {{ formAdd.parentName}}
          </el-form-item>
          <el-form-item label="部门名称" prop="name">
            <el-input v-model="formAdd.name" placeholder="请输入名称"></el-input>
          </el-form-item>
          <el-form-item label="排序值" prop="sort">
            <el-input-number v-model="formAdd.sort" :min="1" :max="100" label="请输入排序"></el-input-number>
            <span style="margin-left:5px">值越小越靠前，支持小数</span>
          </el-form-item>
          <!--
          <el-form-item label="是否启用" prop="status">
            <el-switch v-model="form.status"></el-switch>
          </el-form-item>
          -->
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="modalVisible = false">取消</el-button>
          <el-button type="primary" @click="submitAdd">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import { list, add, update, del } from '@/api/dept'
export default {
  data() {
    return {
      modalTitle: "新增部门",
      modalVisible: false,
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      depts: [],
      form: {
        pid: undefined,
        parentName: undefined,
        id: undefined,
        name: undefined,
        sort: 1
      },
      formAdd: {
        pid: undefined,
        parentName: undefined,
        id: undefined,
        name: undefined,
        sort: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
      },
    }
  },
  methods: {
    getDeptDatas() {
      // const sort = 'id,desc'
      // const params = { sort: sort }
      // if (this.deptName) { params['name'] = this.deptName }
      list().then(res => {
        //console.log(res)
        this.depts = res.data.data
      })
    },
    handleNodeClick(data) {
      //console.log(data)
      this.form.pid = data.pid
      this.form.id = data.id
      this.form.parentName = data.parentName
      this.form.name = data.name
      this.form.sort = data.sort
    },
    save() {
      update(this.form).then(res => {
        let data = res.data;
        if (data.code == 0) {
          this.$message({
            message: '修改成功',
            type: 'success'
          });
          this.getDeptDatas()
        } else {
          this.$message.error(data.msg);
        }
      })
    },
    add() {
      if (this.form.pid != null) {
        this.formAdd.pid = this.form.id
        this.formAdd.parentName = this.form.name
        this.formAdd.id = null
        this.formAdd.name = null
        this.modalVisible = true
      } else {
        this.$message({
          message: '请选择上级部门',
          type: 'warning'
        });
      }
    },
    addRoot() {
      this.formAdd.pid = 0
      this.formAdd.parentName = null
      this.formAdd.id = null
      this.formAdd.name = null
      this.modalVisible = true
    },
    submitAdd() {
      add(this.formAdd).then(res => {
        //console.log(res)
        let data = res.data;
        if (data.code == 0) {
          this.$message({
            message: '新增成功',
            type: 'success'
          });
          this.getDeptDatas()
          this.modalVisible = false;

        } else {
          this.$message.error(data.msg);
        }
      })
    },
    del() {
      this.$confirm('是否删除当前选中部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(this.form.id).then(res => {
          let data = res.data;
          if (data.code == 0) {
            this.getDeptDatas()
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      })
    },
    cancel() {
      this.form = {
        pid: undefined,
        parentName: undefined,
        id: undefined,
        name: undefined,
        sort: 1
      }
    },
  },
  created() {
    this.getDeptDatas()
  }
}
</script>

<style scoped>
.tree {
  margin-top: 20px;
}
</style>

